import OnEventMode from "./OnEventMode";
const { ccclass, property } = cc._decorator;

@ccclass
export default class View extends OnEventMode {

    // 展示对应的UI(Prefab)
    protected show_ui_at(parent: cc.Node, ui_name: string, target?: any, isAnimation?: boolean, callbackEvent?: string, ...args: any) {
        let location = 'panel'
        let name = ui_name
        if (isAnimation) {
            location = 'animations'
            name = 'UIAnimation'
        }
        cc.resources.load(`/prefab/${location}/${ui_name}`, cc.Prefab, (err, prefab) => {
            if (err) {
                console.warn(err)
            } else {
                let node = cc.instantiate(prefab as cc.Prefab)
                node.parent = parent
                node.addComponent(name)
                if (target) node.getComponent(name).init(target, ...args)
                if (callbackEvent) {
                    this.emit(callbackEvent, node)
                }
            }
        })

    }

    //    加载UI面板的全部children
    protected load_all_object(root: cc.Node, path: string) {
        let view = {}
        view = this.loadObj(root, path, view)
        return view
    }

    private loadObj(root: cc.Node, path: string, view: any): any {
        let result = view
        for (let i = 0; i < root.childrenCount; i++) {
            console.log(path + root.children[i].name)
            view[path + root.children[i].name] = root.children[i]
            this.loadObj(root.children[i], path + root.children[i].name + '/', result)
        }
        return result
    }
}
